package com.title3;

import java.io.*;
import java.util.Scanner;

/**
 * @Author milian
 * @Date 2021/8/3 0003 20:47
 */
public class title3 {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入复制文件夹路径：");
        String srcPath = sc.next();
        System.out.println("请输入粘贴文件夹路径：");
        String targePath =sc.next();
        cloneDir(srcPath,targePath);
    }

    private static void cloneDir(String srcPath, String targePath) throws IOException {
        File file = new File(srcPath);
        File[] files = file.listFiles();
        for (File signFile : files) {
            if (signFile.isFile()){
                copyFile(signFile,targePath);
            }else {
                File newTargePath = new File(targePath + "\\" + signFile.getName());
                newTargePath.mkdirs();
                cloneDir(signFile.getPath(),newTargePath.getPath());
            }

        }
    }

    private static void copyFile(File signFile, String targePath) throws IOException {
        String FileName = signFile.getName();
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(signFile.getPath()));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targePath + "\\" + FileName));
        byte[] bytes = new byte[8192];
        int len;
        while ((len=bis.read(bytes))!=-1){
            bos.write(bytes,0,len);
        }
        bis.close();
        bos.close();
    }
}
